using System;
using System.IO;
using ssh;
using dotnet;
using wspace;

namespace command
{
    public class Delete : Cmd
    {
        public Delete()
        {
            this.Command = "delete";
            this.NeedArg = false;
            this.IsWorkAreaCmd = true;
            this.Discript = "Delete new files from a local or remote repository.";
            this.Format = "delete [-s,-l]";
            this.Function = Func;
        }

        private void Func(string[] args)
        {
            if (args.Length != 1 && args.Length != 2)
            {
                Utils.ErrorFormat(this.Format);
                return;
            }
            if (args.Length == 2)
            {
                if (!args[1].Equals("-s") && !args[1].Equals("-l"))
                {
                    Utils.ErrorFormat(this.Format);
                    return;
                }
            }

            ServerInfo sinfo = ServerInfo.GetByConfig(this.Data + Utils.GetSlash() + Configs.ConfigName);
            if (sinfo == null) return;
            SftpConnect brg = new SftpConnect(sinfo);
            if (!brg.Connect()) return;
            sinfo.dir = sinfo.dir.Replace('\\', '/');
            if (sinfo.dir.EndsWith('/')) sinfo.dir = sinfo.dir.Substring(0, sinfo.dir.Length - 1);
            if (!brg.sftp.Exists(sinfo.dir))
            {
                Utils.ErrorLog("The remote folder does not exist.");
                brg.DisConnect();
                return;
            }
            string localpath = Directory.GetParent(this.Data).FullName;
            LocalCmp lcmp = new LocalCmp(brg, sinfo.dir, localpath);
            if (!lcmp.CmpLocalDiff()) return;
            if (args.Length == 2 && args[1].Equals("-l"))
            {
                if (lcmp.LnewFList.Count == 0)
                {
                    Utils.MessageLog("No new files are added to the local repository.");
                    brg.DisConnect();
                    return;
                }
                foreach (string path in lcmp.LnewFList)
                {
                    if (path.EndsWith(Utils.GetSlash()))
                    {
                        try
                        {
                            Directory.Delete(path, true);
                            System.Console.WriteLine("delete folder " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            File.Delete(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                }
                Utils.SuccessLog(lcmp.LnewFList.Count + " local files and folders have been deleted");
                brg.DisConnect();
                return;
            }

            ServerCmp scmp = new ServerCmp(brg, sinfo.dir, localpath);
            if (!scmp.CmpServerDiff(lcmp.LignoreList)) return;
            if (scmp.RnewFList.Count == 0)
            {
                Utils.MessageLog("No new files are added to the remote repository.");
                brg.DisConnect();
                return;
            }

            if (args.Length == 1)
            {
                foreach (string path in lcmp.LnewFList)
                {
                    if (path.EndsWith(Utils.GetSlash()))
                    {
                        try
                        {
                            Directory.Delete(path, true);
                            System.Console.WriteLine("delete folder " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            File.Delete(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                }
                foreach (string path in scmp.RnewFList)
                {
                    if (path.EndsWith('/'))
                    {
                        try
                        {
                            brg.sftp.DeleteDirectory(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            brg.sftp.DeleteFile(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                }
                Utils.SuccessLog("Delete " + lcmp.LnewFList.Count + " local files and " + scmp.RnewFList.Count + " remote files");
            }
            else
            {
                foreach (string path in scmp.RnewFList)
                {
                    if (path.EndsWith('/'))
                    {
                        try
                        {
                            brg.sftp.DeleteDirectory(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            brg.sftp.DeleteFile(path);
                            System.Console.WriteLine("delete file " + path);
                        }
                        catch (Exception e)
                        {
                            Utils.ErrorLog(e.Message);
                            brg.DisConnect();
                            return;
                        }
                    }
                }
                Utils.SuccessLog(scmp.RnewFList.Count + " remote files have been deleted");
            }
            
            brg.DisConnect();

        }
    }
}